package desp.test.inner.demo;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


/**
 * 发送Http Post请求
 * @author wuwenzhe
 *
 */
public class HttpRequestDemo {
//	private static final String POST_URL= "http://172.21.246.89:8099/yxy-web/wp";
//	private static final String POST_URL="http://localhost:7099/yxy-web/yxyAuth"; //本地环境
//  private static final String POST_URL="http://192.168.2.32:7099/yxy-web/yxyAuth"; //王朋本地环境
  private static final String POST_URL="http://192.168.2.182:7099/yxy-web/yxyAuth"; //经信委测试环境
//  private static final String POST_URL="http://172.21.207.177:7099/yxy-web/yxyAuth"; //锡林本地环境
//	private static final String POST_URL="http://172.21.207.228:7099/yxy-web/yxyAuth"; //中堂本地环境
//	private static final String POST_URL="http://144.131.252.11:7099/yxy-web/yxyAuth"; //测试环境
//	private static final String POST_URL="http://144.131.252.138:7099/yxy-web/yxyAuth"; //测试环境
//	private static final String POST_URL="http://172.16.0.109:17099/yxy-web/yxyAuth"; //测试环境
//	private static final String POST_URL="http://172.16.48.7:17099/yxy-web/yxyAuth"; //生产环境
//	private static final String POST_URL="http://11.131.250.100:9200/api/smau"; //环境
	//上线私钥
//	private static final String CHANNEL_PRIVATE_KEY ="30820276020100300d06092a864886f70d0101010500048202603082025c02010002818100a7f0da1834ae70250f7f9cd2eb741f1d246301ec7c304d75f0f27d42bbe5bd593172f33761b6f2b3e10cd313852be5fad5c3f905706c97d6150ede3a168b3f98eb2e5d59cedab37e94bfa97c7c1154aa592708054b095d0e890a3e1df060da4e481dde225a3152ea3cbc69342e81dc6a07f5b934f70ad7e60a20deb222433b010203010001028180738d6832c184c8cae738120a9a5142db455f0e42ce4c59efedc29b67606bb9f5f5ed36e60348e303dbb47c43711cfc57b3dfeca6ce6d19a806c33d2ad0ba2d1e04c05dd376c1323dce1b89a970de744764a1e27cc337c8b7d406dc19ebcb45c5ee2d0086d5f905403449ae4cabaf156f946c7f4da4272c68737dc2a2a4e32df1024100f2ea3eb7dd6549f13153e6c1a534973227e9d7b9cb0f9b46f3ea5ddb41b28d5e00a918f228b166c1373e5a24c5f86c00a1b6d99931fb819283ebafe4815e5f85024100b0fcb9e4758a23bef87d60d0e8b92348d40d773e8d4ee2e2a42bb6502ee76d31ee17a7bde47054ee4e24c5571f4d8e47f91cfa0a600d9184b7a61e265c06804d024100ef06e0c03bf2b757c69a30aecbc97c1db678bbf5ee8264e0e492f1d816458425c3b0a1036857682d28170accb4a1466538bb9c52ca841973faaba860c98bfba102401bc941a205760f909ed0884fa409d4e94d29640c35012a8bac7c945dd84f4b0c68b5dc06eb035bc2221091ced0c24b512f62063b74c30c928a8690fb32bd6e8d02405287cad6ace0b92fbbb6dea16401ea3ac9d340f535afc84fc61ede34ce8ca099cf4cbd5a197ceed894d3d8cc769e40e9c2bb552e59758a47f42417056d160b42";
//	private static final String SMAU_PUB_KEY="30819f300d06092a864886f70d010101050003818d0030818902818100ad54e7b032b17879c58149cda766a8791b359cba02946ef0795649cf1e07aa1dfcc712cf8e4121be770c8fbed858679de335c8df6602cbb068f7e0bfa0d6e36b9d97a6c4143d4c51ad90a0eb6e8d9c861ca2167a3a1e37149ca3a6c70672f60fde3f711e7df9f80cfc048f0c8461c3861fc81bce2eee4e2e51cbb26327b3da290203010001";
	//经信委公私钥
	private static final String CHANNEL_PRIVATE_KEY ="30820278020100300d06092a864886f70d0101010500048202623082025e02010002818100dd73a4761fa9a182df62d667a9df6bd01a6a1bfe86da6572ace9c77fc5f798359f2f51c83186286ba30a3211183b49d046ae793cdb4ab220680821392c8c1720148c9295e836cf5e96b7ddd424460ea1fabda43a29612c606de2bb29c7ecb0744c86659eef6ddcdb867feafb727aa05956f1efb69331eefb4d2e0e5c9b2a74db020301000102818100b7d38117d51cb8c7acab9d131c3e2c7ee90e60029eeab7ffc36da47f7a5417739b9a6a26e68559145098ec0aa8b4be8737c5293acd466a2ae070af3388b9198faa665817fd119a7a2ef05986d0993d084a9875d270fc06c5f47fc53acee066a9ab66c57cb58810b38c4eb4f299bc1799b0bcb2d377a1e0f5f2e29500d348f101024100f1e2a2b5fc17f2b8dcf1d313b0d2363c8de0d2640e154b7f2b3fc6b17cb82199215e5cd77eefa28762d87bff12235abe5f60e70e8cbe3e86058a99b46e482f1b024100ea5fc3677833b8412138ae282067e9d1a61e3c89ed4350b00ac0b97c5e506fd38b86f8f5fe30109e7c178954e805a658a97a692b6f4784ca3a60147260d86d41024100a8165c07a6c565907d4936eb0a072a556e2a7b49b857a7fd192ada82602efc7a132c25cc4293781828e253586f5c4e24d8d6d7f9ce682dbea87f264cb20e4b5d0241009617f1d9a77ee7b6be01c0766e1a5e0126f3222dea463bbc2186bec2c8db9661d16f04acf4b5ef4eaf859b2b46d3a76e0c4b1a62f886c2a187b4d42353a60681024047a7c344821e45ce986467d1154cb8e447576b8f05fda1f6ca09a3eca47b216270edf42751a766fd8e4acf2fd285dfea6621dd41678c47d373eafc7e736fdd33";
	private static final String SMAU_PUB_KEY="30819f300d06092a864886f70d010101050003818d0030818902818100cbc640326d5752b5018d363d3e7dc32c8a5fbde78832ce60e37f118640b4f19d90b31719ac5dc343f3e8ae9e6856b24aa252228ecbe32999061f8ca54e22ae6ae6349ad354220672ff337d14ced0e8917fff6ae3c3a595472179994569622c30c18ba97931cbb0fb74027a13809da0f2ccbf0a9f935eae2ccb29dcdb5fcd91390203010001";

	
	private static final String CONTENT_TYPE_TEXT = "application/json";
	public static void main(String[] args) throws Exception{
		String postMsg=getRequestMsg();
		
		CloseableHttpClient httpclient = HttpClients.custom().build();
        try {

            HttpPost httpPost = new HttpPost(POST_URL);
            System.out.println("Executing request " + httpPost.getRequestLine());
    		StringEntity se = new StringEntity(postMsg, "UTF-8");
    		se.setContentType(CONTENT_TYPE_TEXT);
    		se.setContentEncoding( "UTF-8");
    		httpPost.setEntity(se);
    		System.out.println("test"+httpPost.toString());
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                System.out.println(response.getStatusLine());
                if(200==(response.getStatusLine().getStatusCode())){
	                String respStr=IOUtils.toString(entity.getContent());
	                //进行报文签名的验证
	                System.out.println(respStr);
	                boolean result=PackAndUnpackDemo.verify(respStr, SMAU_PUB_KEY);
	                System.out.println("结果报文签名验证结果为:"+result);
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
	}
	public static String getRequestMsg() throws Exception{
		Map<String,Object> body=new HashMap<String,Object>();
		Map<String,Object> bodys=new HashMap<String,Object>();
		//生产数据
//		bodys.put("certNo", "421083199209300911");//421083199209300911
//		  bodys.put("phoneNo", "18163843884");//	18163843884   15393717182
//		  bodys.put("cardNo", "6212141300000000074");//6212141300000000074   6212141300000000074
//		  bodys.put("name","赵倩测");//赵倩测   赵倩测
//		  bodys.put("certType","01");//01
////		  bodys.put("token","");
////		  bodys.put("message","");
//		  bodys.put("channelId","2000092"); //1029000002
//	      bodys.put("customerId","1234");
//	      bodys.put("userId","00007");
//	      bodys.put("merchantId","5678");
//	      bodys.put("termId","9078");
//	      bodys.put("comb", "dtjd01");
//	      bodys.put("transNum","1");
//	      bodys.put("authFlag","1");
		
		//测试数据
//		  bodys.put("certNo", "110101197609104045");//421083199209300911   421083199209300911
//		  bodys.put("phoneNo", "18163843884");//	18163843884   15393717182
		  bodys.put("cardNo", "6222066629057887");//6212141300000000074   6212141300000000074
//		  bodys.put("name","王刚");//赵倩测   赵倩测
//		  bodys.put("certType","01");//01
//		  bodys.put("token","201804192011322aad1179a02f4e26b139f94a69cf7cb5");//
//		  bodys.put("message","738328");
		  //bodys.put("channelId","2000092"); //改为报文头发送
	      //bodys.put("customerId","1234");//46938143614ce5ee0161d781b3210de5  改为报文头发送
	      bodys.put("userId","3003033");//ZZW201802011814   yrdk002  ytw
	      //bodys.put("merchantId","5678");
	      //bodys.put("termId","9078");
	      bodys.put("comb", "dtjd01");//dtjd01  dtjd02  zzw01    SHHHY201804241424       mchnt01  yrdk01  ytw01  poc02 zzw01
//	      bodys.put("transNum","2");
//	      bodys.put("authFlag","1");
//	      bodys.put("mchntName", "江苏银行股份有限公司"); //商户名称  苏州市相城区联邦时代超市
//	      bodys.put("unionCreditNo","914403001922038216"); //统一信用代码  91320000796544598E
//	      bodys.put("mchntLincenseNo", "43432432423"); // 工商注册号
//	      bodys.put("mchntId", "111"); // //商户id
//	      bodys.put("encryption", "0"); //加密方式
//	      bodys.put("mchntNo", "23232");//商户编号
//	      bodys.put("mealType", "1");
	      body.put("key", bodys);
	     
		//String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10008", "RISK001", "银行卡消费区间验证");
		//String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10008", "RISK002", "银行卡受理习惯验证接口");
		long  st =  System.currentTimeMillis();    
	      String msg= PackAndUnpackDemo.getRequestMsg(body, CHANNEL_PRIVATE_KEY, "10008", "RISK003", "银行卡消费属地占比前三验证接口");
	      System.out.println(System.currentTimeMillis()-st);
		System.out.println(msg);
		return msg;
	}
}